package classes
{
	import classes.Music;
	
	import com.svarlet.fileSearch.FileSearchEvent;
	import com.svarlet.fileSearch.FileSearcher;
	
	import component.search;
	
	import flash.filesystem.File;
	
	import mx.collections.ArrayCollection;
	import mx.events.ListEvent;
	import mx.managers.BrowserManager;

	public class MusicScanner
	{
		private var fs:FileSearcher;
		private var files:ArrayCollection = new ArrayCollection();
		private var supportedExtensions:Array;
		
		public function MusicScanner()
		{
			fs = new FileSearcher();			
			supportedExtensions = [ "mp3", "wav" ];
		}
			 
		protected function fileFoundHandler(event:FileSearchEvent):void
		{
				for each (var ext:String in supportedExtensions)
				{	
					if (event.file.extension == ext)
					{
						files.addItem(new Music(event.file.name, event.file.nativePath));
						trace("Found");
					}
				}	
		}
			 
		protected function searchEndHandler(event:FileSearchEvent):void
		{
			trace("Finished !");
			
			fs.removeEventListener(FileSearchEvent.FILE_FOUND, fileFoundHandler);
			fs.removeEventListener(FileSearchEvent.SEARCH_END, searchEndHandler);
		}	
		
		public function startscan(path:String):void
		{							
			var dir:File = new File();
			dir.nativePath = path;
			files.removeAll();
			
			fs.search(dir);
			fs.addEventListener(FileSearchEvent.FILE_FOUND, fileFoundHandler);
			fs.addEventListener(FileSearchEvent.SEARCH_END, searchEndHandler);			
		}
		
		public function getfiles():ArrayCollection
		{
			return files;
		}
		
		public function removefile(index:int):void
		{
			files.removeItemAt(index);
		}
	}	
}